//
//  XiuGaiMiMaViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "XiuGaiMiMaViewController.h"
#import "PayResultView.h"//支付结果视图
@interface XiuGaiMiMaViewController ()<payResultBackBtnDelegate>

@property (nonatomic, strong)PayResultView *payResultView;

@end

@implementation XiuGaiMiMaViewController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _sureBtn.userInteractionEnabled = NO;
    _sureBtn.selected = NO;
    if (_payResultView) {
        [_payResultView removeFromSuperview];
    }
  
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNaBar];
    self.oldTF.secureTextEntry = YES;
    self.oneTF.secureTextEntry = YES;
    self.twoTF.secureTextEntry = YES;
    [_oldTF addTarget:self action:@selector(handleAction:) forControlEvents:(UIControlEventEditingChanged)];
}
- (void)handleAction:(UITextField *)text {
    _sureBtn.userInteractionEnabled = YES;
    _sureBtn.selected = YES;
}
- (void)configureNaBar {
    self.navigationItem.title = @"修改密码";
    //设置导航栏按钮的边距
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame= CGRectMake(0, 0, 60, 30);
    [rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
    [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacerleft.width = -20;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    
}

- (void)handleBackAxtion {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//确认修改密码的点击事件
- (IBAction)sureBtn:(UIButton *)sender {
    
    if (self.oneTF.text.length && ![self.oneTF.text isEqualToString:self.twoTF.text]) {
        [MBProgressHUD showText:@"新密码两次输入不一致"];
        return;
    }
    
    [self modifyPassword];
    

}
-(void)getPayResultView{
    if (_payResultView) {
        [_payResultView removeFromSuperview];
    }
    _payResultView = [[PayResultView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _payResultView.delegate = self;
    _payResultView.imageStr = @"xiugaimima";
    _payResultView.payResultStr = @"修改成功";
    _payResultView.detailStr = @"已成功为您修改登录密码";
    [[UIApplication sharedApplication].keyWindow addSubview:_payResultView];
}
- (void)payBackAction:(UIButton *)sender {
    if (_payResultView) {
        [_payResultView removeFromSuperview];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)modifyPassword{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"password":_oldTF.text,@"newpassword":_oneTF.text,@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest postWithURLString:POST(Shop_ModifyPassword) parameters:parameDic success:^(id responseObject) {
        
        if ([[responseObject objectForKey:@"ret"] integerValue] != 200) {
            return ;
        }
        if ([[[responseObject objectForKey:@"data"] objectForKey:@"code"] integerValue] == 0) {
            //0表示操作成功 -1原密码错误 -2修改密码失败
            [self getPayResultView];
        }else if ([[[responseObject objectForKey:@"data"] objectForKey:@"code"] integerValue] == -1){
            [MBProgressHUD showText:@"原密码错误"];
        }else{
            [MBProgressHUD showText:@"修改密码失败"];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
}

@end
